/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: connectdialog.cpp 316 2009-02-22 16:14:04Z jpnurmi $
*/

#include "connectdialog.h"
#include "serverdialog.h"
#include <QMessageBox>
#include <QPushButton>
#include <QSettings>

ConnectDialog::ConnectDialog(QWidget* parent, Qt::WindowFlags flags)
    : QDialog(parent, flags)
{
    ui.setupUi(this);

    ui.buttonBox->button(QDialogButtonBox::Ok)->setText(tr("&Connect"));
    connect(ui.buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
        this, SLOT(connectServers()));

    QSettings settings;
    ServerList servers = settings.value("servers").value<ServerList>();
    ui.serverListView->addServers(servers);

    ui.toolButtonNew->setDefaultAction(ui.actionNew);
    ui.toolButtonEdit->setDefaultAction(ui.actionEdit);
    ui.toolButtonDelete->setDefaultAction(ui.actionDelete);

    ui.serverListView->addAction(ui.actionNew);
    ui.serverListView->addAction(ui.actionEdit);
    ui.serverListView->addAction(ui.actionDelete);

    connect(ui.actionNew, SIGNAL(triggered()), this, SLOT(newServer()));
    connect(ui.actionEdit, SIGNAL(triggered()), this, SLOT(editServer()));
    connect(ui.actionDelete, SIGNAL(triggered()), this, SLOT(deleteServers()));

    connect(ui.lineEditFilter, SIGNAL(textEdited(QString)), ui.serverListView, SLOT(setFilter(QString)));

    connect(ui.serverListView->selectionModel(),
        SIGNAL(currentChanged(QModelIndex, QModelIndex)),
        this, SLOT(updateUi()));
    connect(ui.serverListView->selectionModel(),
        SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
        this, SLOT(updateUi()));
    connect(ui.serverListView, SIGNAL(doubleClicked(QModelIndex)),
        this, SLOT(onDoubleClicked(QModelIndex)));

    updateUi();
}

QSize ConnectDialog::sizeHint() const
{
    return QSize(500, 300);
}

void ConnectDialog::done(int result)
{
    
	QSettings settings;
    ServerList servers = ui.serverListView->servers();
    settings.setValue("servers", QVariant::fromValue(servers));
	
    QDialog::done(result);
}

void ConnectDialog::newServer()
{
    Server server = ServerDialog::getServer(this, QApplication::applicationName());
    if (!server.isNull())
        ui.serverListView->addServers(ServerList() << server);
}

void ConnectDialog::editServer()
{
    ServerDialog dialog(this);
    dialog.setServer(ui.serverListView->currentServer());
    if (dialog.exec())
        ui.serverListView->saveCurrentServer(dialog.server());
}

void ConnectDialog::deleteServers()
{
    const int n = ui.serverListView->selectedServers().count();
    if (n > 0)
    {
        QString text = tr("Are you sure you want to delete %n server(s)?", "", n);
        if (QMessageBox::question(this, tr("Confirm"), text, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
            ui.serverListView->deleteServers();
    }
}

void ConnectDialog::connectServers()
{
    ServerList servers = ui.serverListView->selectedServers();
    foreach (Server server, servers)
        emit connectRequested(server);
}

void ConnectDialog::updateUi()
{
    bool hasCurrent = ui.serverListView->currentIndex().isValid();
    int selectionCount = ui.serverListView->selectionModel()->selectedRows().count();
    ui.actionEdit->setEnabled(hasCurrent && selectionCount == 1);
    ui.actionDelete->setEnabled(selectionCount > 0);
    ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(selectionCount > 0);
}

void ConnectDialog::onDoubleClicked(const QModelIndex& index)
{
    //editServer();
    Server server = ui.serverListView->server(index);
    emit connectRequested(server);
    close();
}
